package rabbit.ding;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import rabbit.util.RabbitMqUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: ReceiveLogs
 * Package: rabbit.ding
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-25
 * Time: 21:26
 */

public class ReceiveLogs1 {
    //消息的接收

    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        //获取信道
        Channel chanel = RabbitMqUtils.getChanel();
        //声明一个交换机  参数交换机的名称 和 交换机的类型
        chanel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //声明一个队列 临时队列
        //临时队列 名称名称随机 当消费者断开连接 队列就删除了
        String queue = chanel.queueDeclare().getQueue();
        //交换机和队列之间进行捆绑 参数是 队列 交换机 和 routingKey
        chanel.queueBind(queue, EXCHANGE_NAME, "");
        System.out.println("等待接收消息L1: ");

        //进行消息的接收
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接收到的消息: " + new String(message.getBody()));
        };
        //接收消息的参数 队列 是否自动应答 接收到的回调 取消消费的回调
        chanel.basicConsume(queue, true, deliverCallback, consumerTag -> {});

    }

}
